﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-11>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：抄表数据查询
//********************************************************************************//
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS.ChaXunGL
{
    public partial class ChaoBiaoSJCX : System.Web.UI.Page
    {
        #region 属性

        private int wtype
        {
            get
            {
                return Convert.ToInt32(ViewState["wtype"]);
            }
            set
            {
                ViewState["wtype"] = value;
            }
        }
        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtChaoBiaoSJ
        {
            get
            {
                return ViewState["dtChaoBiaoSJ"] as DataTable;
            }
            set
            {
                ViewState["dtChaoBiaoSJ"] = value;
            }
        }


        #endregion

        #region 变量

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        /// <summary>
        /// 行政区
        /// </summary>
        QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();
        /// <summary>
        /// 行业分类
        /// </summary>
        QS_JX_HANGYEFLXXBLL HYFL = new QS_JX_HANGYEFLXXBLL();
        /// <summary>
        /// 取水用途
        /// </summary>
        QS_JX_QUSHUIXXBLL QSYT = new QS_JX_QUSHUIXXBLL();
        /// <summary>
        /// 管理部门
        /// </summary>
        QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();
        /// <summary>
        /// 流域信息
        /// </summary>
        QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();
        /// <summary>
        /// 水利分片
        /// </summary>
        QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();
        /// <summary>
        /// 水资源分区
        /// </summary>
        QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();
        /// <summary>
        /// 井标记
        /// </summary>
        QS_JX_JINGBIAOJXXBLL JBJ = new QS_JX_JINGBIAOJXXBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            this.gvChaoBiaoSJCX.DataSource = this.dtChaoBiaoSJ;
            this.gvChaoBiaoSJCX.DataBind();

        }




        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            if (txtKaiShiNY.Text != "" && txtJieShuNY.Text != "")
            {
                LoadGridData();
                if (this.dtChaoBiaoSJ == null || this.dtChaoBiaoSJ.Rows.Count < 1)
                {
                    txtShuiBiaoH2.Text = "";
                    txtCeHao.Text = "";
                    txtYongHuH2.Text = "";
                    txtYongHuM2.Text = "";
                    txChaoBiaoRQ.Text = "";
                    txtChaoBiaoZT.Text = "";
                    txtXingZhengQ.Text = "";
                    txtDiZhi.Text = "";
                    txtBenYueCM.Text = "";
                    txtShangYueCM.Text = "";
                    txtJingHao.Text = "";
                    txtQuShuiDD.Text = "";
                    txtBenYueSL.Text = "";
                    txtShuiLiangLB.Text = "";
                    txtLaoJingH.Text = "";
                    txtShuiJia2.Text = "";
                    memoBeiZhu.Text = "";
                    zjl.Text = "共0条";
                }
            }
            else
            {
                this.gvChaoBiaoSJCX.DataSource = null;
                this.gvChaoBiaoSJCX.DataBind();
            }
        }


        /// <summary>
        /// tab1导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (this.dtChaoBiaoSJ == null || this.dtChaoBiaoSJ.Rows.Count < 1)
            {
                PageTools.AlertMsg(this, "没有符合的数据导出");
            }
            else
            {
                PageTools.ExportExcel(this.dtChaoBiaoSJ);
            }
        }


        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
             this.wtype = BusinessTool.WType;
            gvChaoBiaoSJCX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            string water_y_m = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();

            if (wtype == 1)
            {
                rabJingBianH.Text = "取水口编号";
                rabShuiLiangLB.ClientVisible = false;
                cmbShuiLiangLB.ClientVisible = false;
                rabGuanLiBM.ClientVisible = false;
                cbmGuanLiBM.ClientVisible = false;
                jh.InnerText = "取水口号：";
                jh2.InnerText = "";
                txtLaoJingH.ClientVisible = false;
            }
            else
            {
                rabJingBianH.Text = "井编号";
                jh.InnerText = "井号：";
                jh2.InnerText = "老井号：";
            }
            //业务员
            //LoadYWY();
            txtKaiShiNY.Text = water_y_m;
            txtJieShuNY.Text = water_y_m;
            //加载管理部门
            LoadGLBM();
            //加载行政区
            LoadXZQ();
            //加载业务员
            LoadYWY(wtype);
            //加载水量类别
            LoadSLLB();
            
        }



        /// <summary>
        /// 加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            DataTable dt = GLBM.SelectQS_JX_BUMENXXesDynamic("I_JLZT=0 and S_BUMENBH NOT IN(0,600)", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cbmGuanLiBM.Items.Add(dt.Rows[i]["S_BUMENMC"].ToString(), dt.Rows[i]["S_BUMENBH"]);
            }
            cbmGuanLiBM.Items.Insert(0, new ListEditItem("", "0"));

        }

        /// <summary>
        /// 加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            DataTable dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
            }
        }

        /// <summary>
        /// 加载业务员
        /// </summary>
        private void LoadYWY( int wtype)
        {
            if (wtype==2)
            {
                cmbYeWuY.Items.Add("",0);
                cmbYeWuY.Items.Add("秦福祥",111);
                cmbYeWuY.Items.Add("李东明", 117);
            }
            else
            {
                PageTools.QSLoadCombox(cmbYeWuY, PageTools.eLoadType.业务员QS, null, "");
            } 
            
        }


        /// <summary>
        /// 加载水量类别
        /// </summary>
        private void LoadSLLB()
        {
            string[] arr1 = {"", "普通采", "采灌采", "采灌灌", "矿泉采", "专灌" };
            string[] arr2 = { "0","1", "2", "3", "4", "5" };

            for (int i = 0; i < arr1.Length; i++)
            {
                cmbShuiLiangLB.Items.Add(arr1[i].ToString(), arr2[i]);
            }
        }
        
        /// <summary>
        /// 加载查询数据
        /// </summary>
        private void LoadGridData()
        {
            int wtype = BusinessTool.WType;
            int ksny = Convert.ToInt32(txtKaiShiNY.Text);
            int jsny = Convert.ToInt32(txtJieShuNY.Text);
            int hnhw = cmbHuanNeiHW.Value != null ? Convert.ToInt32(cmbHuanNeiHW.Value) : 0;
            string xzq = cmbXingZhengQ.Text != "全部" ? cmbXingZhengQ.Value.ToString() : null;
            string ywy = cmbYeWuY.SelectedIndex > 0 ? cmbYeWuY.Value.ToString() : null;
            int v_bs = -1;
            string v_val = null;
            //1 用户号，2 井号，3 水表号，4 用户名，5 用户地址 ，6 取水地点 ，
            //7 水量类别 ，8 水价，9 水量，10 抄表状态，11 主管部门，12 本月/上月，
            if (chkYongHuH.Checked && txtYongHuH.Text != "")
            {
                v_bs = 1;
                v_val = txtYongHuH.Text;
            }
            else if (rabJingBianH.Checked && txtJingBianH.Text != "")
            {
                v_bs = 2;
                v_val = txtJingBianH.Text;
            }
            else if (rabShuiBiaoH.Checked && txtShuiBiaoH.Text != "")
            {
                v_bs = 3;
                v_val = txtShuiBiaoH.Text;
            }
            else if (chkYongHuM.Checked && txtYongHuM.Text != "")
            {
                v_bs = 4;
                v_val = txtYongHuM.Text;
            }
            else if (chkYongHuDZ.Checked && txtYongHuDZ.Text != "")
            {
                v_bs = 5;
                v_val = txtYongHuDZ.Text;
            }
            else if (rabQuShuiDZ.Checked && txtQuShuiDZ.Text != "")
            {
                v_bs = 6;
                v_val = txtQuShuiDZ.Text;
            }
            else if (rabShuiLiangLB.Checked && cmbShuiLiangLB.Text != "")
            {
                v_bs = 7;
                v_val = cmbShuiLiangLB.Value.ToString();
            }
            else if (rabShuiJia.Checked && txtShuiJia.Text != "")
            {
                v_bs = 8;
                v_val = txtShuiJia.Text;
            }
            else if (rabShuiLiang.Checked && cmbShuiLiang.Text != "" && txtShuiLiang.Text!="")
            {
                v_bs = 9;
                v_val = cmbShuiLiang.Text + txtShuiLiang.Text;
            }
            else if (rabChaoBiaoZT.Checked && cmbChaoBiaoZT.Text != "")
            {
                v_bs = 10;
                v_val = cmbChaoBiaoZT.Value.ToString();
            }
            else if (rabGuanLiBM.Checked && cbmGuanLiBM.Text != "")
            {
                v_bs = 11;
                v_val = cbmGuanLiBM.Value.ToString();
            }
            else if (rabBenYueSY.Checked && cmbBenYueSY.Text != "" && txtBenYueSY.Text!="")
            {
                v_bs = 12;
                v_val = cmbBenYueSY.Text.ToString() + txtBenYueSY.Text;
            }
            this.dtChaoBiaoSJ = SearchSpHelper.P_QSCX_CHAOBIAOSJCX(wtype, ksny, jsny, hnhw, xzq, ywy, v_bs, v_val, 0).Tables[0];
            if (wtype == 1)
            {
                gvChaoBiaoSJCX.Columns.Clear();
                LoadColumnDate(gvChaoBiaoSJCX, "CBNY", "抄表年月", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_YONGHUH", "用户号", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_YONGHUM", "用户名", 200);
                LoadColumnDate(gvChaoBiaoSJCX, "S_JINGBH", "取水口号", 70);
                LoadColumnDate(gvChaoBiaoSJCX, "S_SHUIBIAOBH", "水表号", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "S_XINGZHENGQM", "行政区名", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_CEHAO", "册号", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "I_BENYUECM", "本月抄码", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "I_SYCM", "上月抄码", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "I_BENYUESL", "本月水量", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "D_TIANJIARQ", "抄表日期", 85, "yyyy-MM-dd");
                LoadColumnDate(gvChaoBiaoSJCX, "CBZT", "抄表状态", 70);
                LoadColumnDate(gvChaoBiaoSJCX, "N_SHUIJIA", "水价", 70, "0.00");
                LoadColumnDate(gvChaoBiaoSJCX, "S_BIAOFENL", "表分类", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_DIZHI", "用户地址", 200);
                LoadColumnDate(gvChaoBiaoSJCX, "S_JINGDIZ", "取水地点", 200);
                LoadColumnDate(gvChaoBiaoSJCX, "S_YEWUY", "业务员", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "D_CAOZUORQ", "修改日期", 85, "yyyy-MM-dd");
                LoadColumnDate(gvChaoBiaoSJCX, "I_SYSL", "上月水量", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "N_BY_SY", "本月/上月水量", 90);

                LoadColumnDate(gvChaoBiaoSJCX, "I_DRBENYUECM", "导入抄码", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "I_DRBENYUESL", "导入水量", 90);
            }
            else
            {
                gvChaoBiaoSJCX.Columns.Clear();
                LoadColumnDate(gvChaoBiaoSJCX, "CBNY", "抄表年月", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_YONGHUH", "用户号", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_YONGHUM", "用户名", 200);
                LoadColumnDate(gvChaoBiaoSJCX, "S_LAOBIANH", "原井号", 70);
                LoadColumnDate(gvChaoBiaoSJCX, "S_JINGBH", "井号", 70);
                LoadColumnDate(gvChaoBiaoSJCX, "S_SHUIBIAOBH", "水表号", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "S_XINGZHENGQM", "行政区名", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_CEHAO", "册号", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "SLLB", "水量类别", 70);
                LoadColumnDate(gvChaoBiaoSJCX, "I_BENYUECM", "本月抄码", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "I_SYCM", "上月抄码", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "I_BENYUESL", "本月水量", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "D_TIANJIARQ", "抄表日期", 85, "yyyy-MM-dd");
                LoadColumnDate(gvChaoBiaoSJCX, "CBZT", "抄表状态", 70);
                LoadColumnDate(gvChaoBiaoSJCX, "N_SHUIJIA", "水价", 70, "0.00");
                LoadColumnDate(gvChaoBiaoSJCX, "S_BIAOFENL", "表分类", 60);
                LoadColumnDate(gvChaoBiaoSJCX, "S_DIZHI", "用户地址", 200);
                LoadColumnDate(gvChaoBiaoSJCX, "S_JINGDIZ", "取水地点", 200);
                LoadColumnDate(gvChaoBiaoSJCX, "S_BUMENMC", "主管部门", 100);
                LoadColumnDate(gvChaoBiaoSJCX, "S_YEWUY", "业务员", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "D_CAOZUORQ", "修改日期", 85, "yyyy-MM-dd");
                LoadColumnDate(gvChaoBiaoSJCX, "I_SYSL", "上月水量", 80);
                LoadColumnDate(gvChaoBiaoSJCX, "N_BY_SY", "本月/上月水量", 90);
            }
            gvChaoBiaoSJCX.DataSource = this.dtChaoBiaoSJ;
            gvChaoBiaoSJCX.DataBind();
            if (this.dtChaoBiaoSJ != null && this.dtChaoBiaoSJ.Rows.Count > 0)
            {
                zjl.Text = "共" + this.dtChaoBiaoSJ.Rows.Count.ToString() + "条";
                gvChaoBiaoSJCX.FocusedRowIndex = -1;
                gvChaoBiaoSJCX.FocusedRowIndex = 0;
                SetData(ksny, jsny);
            }

        }

        /// <summary>
        /// 给显示框赋值
        /// </summary>
        private void SetData(int ksny, int jsny)
        {
            DataTable dt_temp = SearchSpHelper.P_QSCX_CHAOBIAOSJCX(BusinessTool.WType, ksny, jsny, 0, "0", "0", -1, null, Convert.ToInt32(gvChaoBiaoSJCX.GetRowValues(0, "ID"))).Tables[0];
            if (dt_temp != null && dt_temp.Rows.Count > 0)
            {
                txtShuiBiaoH2.Text = dt_temp.Rows[0]["S_SHUIBIAOBH"].ToString();
                txtCeHao.Text = dt_temp.Rows[0]["S_CEHAO"].ToString();
                txtYongHuH2.Text = dt_temp.Rows[0]["S_YONGHUH"].ToString();
                txtYongHuM2.Text = dt_temp.Rows[0]["S_YONGHUM"].ToString();
                txChaoBiaoRQ.Text = dt_temp.Rows[0]["D_TIANJIARQ"].ToString();
                txtChaoBiaoZT.Text = dt_temp.Rows[0]["CBZT"].ToString();
                txtXingZhengQ.Text = dt_temp.Rows[0]["S_XINGZHENGQM"].ToString();
                txtDiZhi.Text = dt_temp.Rows[0]["S_DIZHI"].ToString();
                txtBenYueCM.Text = dt_temp.Rows[0]["I_BENYUECM"].ToString();
                txtShangYueCM.Text = dt_temp.Rows[0]["I_SYCM"].ToString();
                txtJingHao.Text = dt_temp.Rows[0]["S_JINGBH"].ToString();
                txtQuShuiDD.Text = dt_temp.Rows[0]["S_JINGDIZ"].ToString();
                txtBenYueSL.Text = dt_temp.Rows[0]["I_BENYUESL"].ToString();
                txtShuiLiangLB.Text = dt_temp.Rows[0]["SLLB"].ToString();
                txtLaoJingH.Text = dt_temp.Rows[0]["S_LAOBIANH"].ToString();
                txtShuiJia2.Text = dt_temp.Rows[0]["N_SHUIJIA"].ToString();
                memoBeiZhu.Text = dt_temp.Rows[0]["S_ZHUSHI"].ToString();
            }
        }

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int ksny = Convert.ToInt32(txtKaiShiNY.Text);
            int jsny = Convert.ToInt32(txtJieShuNY.Text);
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            if (e.Parameter == "setinfo")
            {
                string msg = "";
                if (this.dtChaoBiaoSJ != null)
                {
                    DataTable dt_temp = SearchSpHelper.P_QSCX_CHAOBIAOSJCX(BusinessTool.WType, ksny, jsny, 0, "0", "0", -1, null, id).Tables[0];

                    msg = string.Join("|",
                    dt_temp.Rows[0]["S_SHUIBIAOBH"].ToString(),
                    dt_temp.Rows[0]["S_CEHAO"].ToString(),
                    dt_temp.Rows[0]["S_YONGHUH"].ToString(),
                    dt_temp.Rows[0]["S_YONGHUM"].ToString(),
                    dt_temp.Rows[0]["D_TIANJIARQ"].ToString(),
                    dt_temp.Rows[0]["CBZT"].ToString(),
                    dt_temp.Rows[0]["S_XINGZHENGQM"].ToString(),
                    dt_temp.Rows[0]["S_DIZHI"].ToString(),
                    dt_temp.Rows[0]["I_BENYUECM"].ToString(),
                    dt_temp.Rows[0]["I_SYCM"].ToString(),
                    dt_temp.Rows[0]["S_JINGBH"].ToString(),
                    dt_temp.Rows[0]["S_JINGDIZ"].ToString(),
                    dt_temp.Rows[0]["I_BENYUESL"].ToString(),
                     dt_temp.Rows[0]["SLLB"].ToString(),
                    dt_temp.Rows[0]["S_LAOBIANH"].ToString(),
                    dt_temp.Rows[0]["N_SHUIJIA"].ToString(),
                    dt_temp.Rows[0]["S_ZHUSHI"].ToString()
                      );
                }

                e.Result = e.Parameter + "|0|" + msg;
            }
        }
        #endregion

        private void LoadColumnDate(ASPxGridView gv, string fieldName, string caption, int width)
        {
            GridViewDataTextColumn c = new GridViewDataTextColumn();
            c.FieldName = fieldName;
            c.Caption = caption;
            c.Width = width;
            gv.Columns.Add(c);
        }

        private void LoadColumnDate(ASPxGridView gv, string fieldName, string caption, int width, string format)
        {
            GridViewDataTextColumn c = new GridViewDataTextColumn();
            c.FieldName = fieldName;
            c.Caption = caption;
            c.Width = width;
            c.PropertiesEdit.DisplayFormatString = format;
            gv.Columns.Add(c);
        }

        protected void gvChaoBiaoSJCX_HtmlRowPrepared(object sender, ASPxGridViewTableRowEventArgs e)
        {

            if (e.RowType != GridViewRowType.Data) return;
            
            if (dtChaoBiaoSJ != null && dtChaoBiaoSJ.Rows.Count > 0)
            {
                if (this.wtype == 2)
                {
                    double s = Convert.ToDouble(e.GetValue("N_BY_SY"));
                    if (s > 1.10)
                    {
                        //e.Row.BackColor = this.wtype == 2 ? System.Drawing.Color.Red : System.Drawing.Color.LightPink;
                        e.Row.BackColor = System.Drawing.Color.Red;
                    }
                    else if (s < 0.9)
                    {
                        //e.Row.BackColor = this.wtype == 2 ? System.Drawing.Color.LightBlue : System.Drawing.Color.LightGreen;
                        e.Row.BackColor = System.Drawing.Color.LightBlue;
                    }
                }
               
            }

        }



        protected void btnExport_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.GridViewID = "gvChaoBiaoSJCX";
            string sName = txtKaiShiNY.Text + "到" + txtJieShuNY.Text + "抄表数据";
            gvChaoBiaoSJCX.Settings.ShowFooter = false;
            ASPxGridViewExporter1.WriteXlsToResponse(sName);
     
        }
    }
}